package com.ustc.mobilelib.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.ustc.diven.model.R;
import com.ustc.mobilelib.control.RequestHelper;
import com.ustc.mobilelib.model.ConstDef;
import com.ustc.mobilelib.model.DataCenter;

public class SearchActivity extends Activity{
	
	private static final String TAG = SearchActivity.class.toString();
	private EditText searchKeyword = null;
	private ImageButton search = null;
	private String keyword = null;
	
	private RelativeLayout loading = null;
	private static final int BOOK_QUERY_COMPLETE = 1; 
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			Intent intent = null;
			switch (msg.what) 
			{
			
			case BOOK_QUERY_COMPLETE:
				loading.setVisibility(View.INVISIBLE);
				intent = new Intent(SearchActivity.this, BookListActivity.class);
				startActivity(intent);
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK)
		{
			logoutDialog();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void logoutDialog() {
		AlertDialog.Builder builder = new Builder(SearchActivity.this);
//		builder.setMessage(getString(R.string.logout_confirm));
		builder.setIcon(R.drawable.logo);
		builder.setTitle(getString(R.string.logout_confirm));
		builder.setCancelable(false);
		
		builder.setPositiveButton(getString(R.string.confirm), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
//				MyLibActivity.this.finish();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		
		builder.setNegativeButton(getString(R.string.cancel), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		init();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void init()
	{
		this.getWindow().setBackgroundDrawableResource(R.color.loading_bg);
		loading = (RelativeLayout) findViewById(R.id.loading);
		searchKeyword = (EditText) findViewById(R.id.search_text);
		search = (ImageButton) findViewById(R.id.search_button);
		search.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DataCenter.getInstance().setCurrUsedBufferID(1);
				keyword = searchKeyword.getText().toString();
				if(!keyword.equals(""))
				{
					new Thread(queryRunnable).start();
					searchKeyword.setText("");
					loading.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	private Runnable queryRunnable = new Runnable()
	{

		@Override
		public void run() 
		{
				RequestHelper.postQueryMsg(keyword);	
				byte[] imgStream = null;
				for(int i = 0; i < 4; i++)
				{
					if(!isImgCache(i))
					{
						String imgURL = new StringBuffer(ConstDef.BOOK_IMG_URL_PREFIX).append(DataCenter.getInstance().getQueryBookResultBuffer().get(i).getImgHref()).toString();
						try 
						{
							imgStream = RequestHelper.getBookImageStream(imgURL);
							DataCenter.getInstance().getQueryRusultBookItemImageBuffer().put(i, imgStream);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				mHandler.obtainMessage(BOOK_QUERY_COMPLETE).sendToTarget();
		}
	};
	private boolean isImgCache(int itemNumber)
	{
		if(DataCenter.getInstance().getQueryMostPopBookItemImageBuffer().get(itemNumber) != null)
		{
			return true;
		}
		return false;
	}
}
